package com.cll.learn.rabbitmq.controller;

import com.cll.learn.rabbitmq.entity.Goods;
import com.cll.learn.rabbitmq.entity.Order;
import com.cll.learn.rabbitmq.service.OrderService;
import com.cll.learn.rabbitmq.util.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2021/2/3 15:57
 */
@Controller
@RequestMapping(value = "/rabbitmq/order")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    OrderService orderService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @RequestMapping(value = "/list")
    public String list(Model model){
        List<Order> orderList = orderService.listAll();
        logger.info("===>>> 订单列表 = {}", orderList);
        model.addAttribute("orderList", orderList);
        return "order/list";
    }

    @PostMapping(value = "/submitOrder")
    @ResponseBody
    public AjaxResult<String> submitOrder(@RequestParam(value = "orderId") String orderId){
        try {
            return orderService.submitOrder(orderId);
        } catch (Exception e) {
            logger.error("****** 提交订单抛出异常" + e.getMessage(), e);
            return new AjaxResult<>(500, "系统错误");
        }
    }

    @ResponseBody
    @PostMapping(value = "/cancelOrder")
    public AjaxResult<String> cancelOrder(@RequestParam(value = "orderId") String orderId){
        try {
            return orderService.cancelOrder(orderId);
        } catch (Exception e) {
            logger.error("****** 提交订单抛出异常" + e.getMessage(), e);
            return new AjaxResult<>(500, "系统错误");
        }
    }
}